
#include <coroutine>
#include <iostream>

// 简单的生成器协程
template<typename T>
struct Generator {
    struct promise_type {
        T current_value;
        
        Generator get_return_object() {
            return Generator{std::coroutine_handle<promise_type>::from_promise(*this)};
        }
        
        std::suspend_always initial_suspend() { return {}; }
        std::suspend_always final_suspend() noexcept { return {}; }
        
        std::suspend_always yield_value(T value) {
            current_value = value;
            return {};
        }
        
        void return_void() {}
        void unhandled_exception() { std::terminate(); }
    };
    
    std::coroutine_handle<promise_type> coro;
    
    explicit Generator(std::coroutine_handle<promise_type> h) : coro(h) {}
    ~Generator() { if (coro) coro.destroy(); }
    
    bool next() {
        coro.resume();
        return !coro.done();
    }
    
    T value() {
        return coro.promise().current_value;
    }
};

// 使用协程的数字生成器
Generator<int> number_generator() {
    for (int i = 1; i <= 5; ++i) {
        std::cout << "生成数字: " << i << std::endl;
        co_yield i;  // 暂停并返回i
    }
}

int main() {
    auto gen = number_generator();
    
    while (gen.next()) {
        std::cout << "收到数字: " << gen.value() << std::endl;
    }
    
    return 0;
}
